package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.DeviceCategoryAdjustParameterMapper;
import com.zhijian.medical.entity.po.Device;
import com.zhijian.medical.entity.po.DeviceCategoryAdjustParameter;
import com.zhijian.medical.entity.vo.request.DeviceCategoryAdjustParameterSearchVo;
import com.zhijian.medical.entity.vo.response.DeviceCategoryAdjustParameterVo;
import com.zhijian.medical.service.DeviceCategoryAdjustParameterService;
import com.zhijian.medical.service.DeviceService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 设备品类校准参数业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-01-17 15:45:16
 */
@Slf4j
@Service
public class DeviceCategoryAdjustParameterServiceImpl implements DeviceCategoryAdjustParameterService {

    @Autowired
    private DeviceCategoryAdjustParameterMapper mapper;

    @Autowired
    private DeviceService deviceService;

    @Override
    public DeviceCategoryAdjustParameter selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceCategoryAdjustParameterVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByCategoryId(Long categoryId) {
        mapper.deleteByCategoryId(categoryId);
    }

    @Override
    public Long insert(DeviceCategoryAdjustParameter deviceCategoryAdjustParameter) {
        Date now = new Date();
        deviceCategoryAdjustParameter.setCreateTime(now);
        deviceCategoryAdjustParameter.setUpdateTime(now);
        return mapper.insert(deviceCategoryAdjustParameter) > 0 ? deviceCategoryAdjustParameter.getId() : 0L;
    }

    @Override
    public boolean update(DeviceCategoryAdjustParameter deviceCategoryAdjustParameter) {
        Date now = new Date();
        deviceCategoryAdjustParameter.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceCategoryAdjustParameter) != 0;
    }

    @Override
    public Integer count(DeviceCategoryAdjustParameterSearchVo deviceCategoryAdjustParameterSearchVo) {
        return mapper.count(deviceCategoryAdjustParameterSearchVo);
    }

    @Override
    public List<DeviceCategoryAdjustParameterVo> selectList(DeviceCategoryAdjustParameterSearchVo deviceCategoryAdjustParameterSearchVo) {
        return mapper.selectList(deviceCategoryAdjustParameterSearchVo);
    }

    @Override
    public Page<DeviceCategoryAdjustParameterVo> selectPage(DeviceCategoryAdjustParameterSearchVo deviceCategoryAdjustParameterSearchVo) {
        //先直接查总数
        int count = count(deviceCategoryAdjustParameterSearchVo);
        //分页
        Page<DeviceCategoryAdjustParameterVo> page = new Page<>(true, count, deviceCategoryAdjustParameterSearchVo.getPageNum(), deviceCategoryAdjustParameterSearchVo.getPageSize());
        deviceCategoryAdjustParameterSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceCategoryAdjustParameterVo> responseVos = this.selectList(deviceCategoryAdjustParameterSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public List<DeviceCategoryAdjustParameterVo> selectByDeviceId(Long deviceId) {
        Device device = deviceService.selectById(deviceId);
        if (Objects.isNull(device) || Objects.isNull(device.getCategoryId())) {
            return new LinkedList<>();
        }
        return this.selectList(DeviceCategoryAdjustParameterSearchVo.builder()
                .categoryId(device.getCategoryId())
                .build());
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean saveAll(List<DeviceCategoryAdjustParameter> deviceCategoryAdjustParameters) {
        AssertUtil.check(CollectionUtil.isEmpty(deviceCategoryAdjustParameters), "记录不能为空");
        this.deleteByCategoryId(deviceCategoryAdjustParameters.get(0).getCategoryId());
        deviceCategoryAdjustParameters.forEach(this::insert);
        return true;
    }

}